package com.magictower.primary.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Xml;

import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileOutputStream;

/**
 * 短信备份工具类
 *
 * @author kejunzhan
 * @since 2017/12/17
 */

public class SMSUitls {
    private static final String seed = "1234567890";

    /**
     * 备份短信的回调接口
     */
    public interface BackUpCallBackSms {
        void before(int count);

        void onBackUpSms(int progress);
    }

    public static boolean backup(Context context, BackUpCallBackSms callback) {
        /**
         * 备份短信
         * 1.判断当前是否存在SD卡
         * 2.权限——使用内容观察者
         * 3.写短信到SD卡中
         */
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            ContentResolver resolver = context.getContentResolver();
            Uri uri = Uri.parse("content://sms/");
            //type = 1 接收短信
            //type = 2 发送短信
            Cursor cursor = resolver.query(uri, new String[]{"address", "date", "type", "body"}, null, null, null);
            if (cursor == null) {
                //若为空，停止操作
                return false;
            }
            int count = cursor.getCount();
            //查询数据库后回调
            callback.before(count);
            int progress = 0;
            // 输出文件
            try {
                File file = new File(Environment.getExternalStorageDirectory(), "backupSms.xml");
                FileOutputStream os = new FileOutputStream(file);
                // 得到序列解析器
                // 在android所有有关都是pull解析
                XmlSerializer serializer = Xml.newSerializer();
                //设置输出编码格式
                serializer.setOutput(os, "utf-8");
                //设置头文件的编码格式，standalone: true表示文件独立, false反之
                serializer.startDocument("utf-8", true);
                //设置头标签
                serializer.startTag(null, "smss");
                //设置上面标签的属性
                serializer.attribute(null, "size", String.valueOf(count));

                while (cursor.moveToNext()) {
                    System.err.println("----------------------------");
                    System.out.println("address = " + cursor.getString(0));
                    System.out.println("date = " + cursor.getString(1));
                    System.out.println("type = " + cursor.getString(2));
                    System.out.println("body = " + cursor.getString(3));
                    serializer.startTag(null, "sms");
                    serializer.startTag(null, "address");
                    serializer.text(cursor.getString(0));
                    serializer.endTag(null, "address");
                    serializer.startTag(null, "date");
                    serializer.text(cursor.getString(1));
                    serializer.endTag(null, "date");
                    serializer.startTag(null, "type");
                    serializer.text(cursor.getString(2));
                    serializer.endTag(null, "type");
                    serializer.startTag(null, "body");
                    //加密短信内容
                    serializer.text(Crypto.encrypt(seed, cursor.getString(3)));
                    serializer.endTag(null, "body");
                    serializer.endTag(null, "sms");
                    progress++;
                    callback.onBackUpSms(progress);
                    SystemClock.sleep(200);
                }
                serializer.endTag(null, "smss");
                serializer.endDocument();
                //关闭查询集
                cursor.close();
                //关闭流
                os.flush();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        } else {
            return false;
        }
    }
}
